import { dispatch, getState, model } from 'tva';

import os from '@packages/lib-os';

import { reportError } from './report';

const typeOf = (obj) => Object.prototype.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();

// 对象浅拷贝
const shallowMerge = (oldObj = {}, newObj = {}) => {
    // 把相同的key merge起来
    const obj = {};
    Object.keys(oldObj).forEach((i) => {
        if (typeOf(oldObj[i]) === 'object' && typeOf(newObj[i]) === 'object') {
            obj[i] = {
                ...oldObj[i],
                ...newObj[i],
            };
        }
    });

    const mergeObj = {
        ...oldObj,
        ...newObj,
        ...obj,
    };

    // test case
    // if (mergeObj.sale.uri !== 10) {
    //     reportError({ type: 'shallowMerge-error', state:mergeObj });
    // }

    return mergeObj;
};

const name = 'testTva';

model({
    namespace: name,
    state: { sale: {}, bid: {}, shop: {} },
    reducer: {
        setState: (draft, { sale = {}, bid = {}, shop = {}, lock = false }) => {
            const q = shallowMerge(draft, { sale, bid, shop });

            if (lock && (q.sale.uri !== 10 || !q.shop.test)) {
                // merge不符合预期
                reportError({ type: 'shallowMerge-error', state: q });
            }

            draft.sale = q.sale;
            draft.shop = q.shop;
            draft.bid = q.bid;
        },
    },
    effect: {
        first: (_, { put }) => {
            put({
                type: 'setState',
                sale: { uri: 1 },
                shop: { uri: 2 },
                bid: { uri: 3 },
            });
        },
        second: (_, { put }) => {
            put({
                type: 'setState',
                sale: { uri: 4, test: 'test' },
                shop: { uri: 5 },
                bid: { uri: 6, showPrice: 123 },
            });
        },
        third: (_, { put }) => {
            setTimeout(() => {
                put({
                    type: 'setState',
                    sale: { uri: 7 },
                    shop: { uri: 8, test: 'test' },
                    bid: { uri: 9, endTime: 123456 },
                });
            });
        },
        fourth: ({ callback }, { put }) => {
            put({
                type: 'setState',
                sale: { uri: 10 },
                shop: { uri: 11, test: 'test' },
                bid: { uri: 12, test: 'test' },
                lock: true
            });
            callback();
        },
    },
});

const [, webkitVersion = ''] = navigator.userAgent.match(/AppleWebKit\/(\S+)/) || [];

if (webkitVersion.split('.')[0] < 604 && os.ios) {
    // safari 10 以下的机子
    // if (!Object.getOwnPropertyDescriptors) {
    //     reportError({ type: 'getOwnPropertyDescriptors-error' });
    // }
    dispatch({
        type: `${name}/first`,
    });
    // dispatch({
    //     type: `${name}/third`,
    // });
    dispatch({
        type: `${name}/fourth`,
        callback: () => {
            const state = getState().testTva;
            if (state.sale.uri !== 10) {
                // 结果不符合预期，则上报
                reportError({ type: 'tva-error', state });
            }
        },
    });
}
